/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package JANDSL.impl;

import JANDSL.JANDSLPackage;
import JANDSL.Transformer;

import MocaTree.Node;

import SDMLanguage.activities.ActivitiesFactory;
import SDMLanguage.activities.Activity;
import SDMLanguage.activities.ActivityEdge;
import SDMLanguage.activities.EdgeGuard;
import SDMLanguage.activities.StartNode;
import SDMLanguage.activities.StopNode;
import SDMLanguage.activities.StoryNode;

import SDMLanguage.patterns.LinkVariable;
import SDMLanguage.patterns.BindingOperator;
import SDMLanguage.patterns.BindingSemantics;
import SDMLanguage.patterns.BindingState;
import SDMLanguage.patterns.ObjectVariable;
import SDMLanguage.patterns.PatternsFactory;
import SDMLanguage.patterns.StoryPattern;

import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import de.upb.tools.sdm.*;

import java.util.*;

import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EParameter;
import org.eclipse.emf.ecore.EcoreFactory;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Transformer</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class TransformerImpl extends EObjectImpl implements Transformer {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TransformerImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return JANDSLPackage.Literals.TRANSFORMER;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void transform(EPackage pack, Node in) {
		boolean fujaba__Success = false;
		Iterator fujaba__IterNodeWidthToWidth = null;
		Object _TmpObject = null;
		Node width = null;
		Iterator fujaba__IterNodeHeightToHeight = null;
		Node height = null;
		Iterator fujaba__IterBoardToNodeWidth = null;
		Node nodeWidth = null;
		Iterator fujaba__IterBoardToNodeHeight = null;
		Node nodeHeight = null;
		Iterator fujaba__IterInToBoard = null;
		Node board = null;
		Iterator fujaba__IterInToFigures = null;
		Node Figures = null;
		Iterator fujaba__IterGameLogicClazzToAttrWidth = null;
		EAttribute attrWidth = null;
		Iterator fujaba__IterGameLogicClazzToAttrHeight = null;
		EAttribute attrHeight = null;
		Iterator fujaba__IterPackToFigClazz = null;
		EClass figClazz = null;
		Iterator fujaba__IterPackToFieldClazz = null;
		EClass fieldClazz = null;
		Iterator fujaba__IterPackToGameLogicClazz = null;
		EClass GameLogicClazz = null;
		EAnnotation eAnnotation = null;
		Activity activity = null;
		StoryPattern ruleStory = null;
		Iterator fujaba__IterLastMoveToNoLink = null;
		LinkVariable noLink = null;
		Iterator fujaba__IterRuleStoryToLastMove = null;
		ObjectVariable lastMove = null;
		ObjectVariable newMove = null;
		LinkVariable newLinkVar = null;
		Iterator fujaba__IterPackToFigureClazz = null;
		EClass figureClazz = null;
		ObjectVariable figureToDestroy = null;
		Iterator fujaba__IterPackToFigureClass = null;
		EClass figureClass = null;
		ObjectVariable noFigure = null;
		Iterator fujaba__IterRuleSetToMove = null;
		Node move = null;
		Iterator fujaba__IterRuleToRuleSet = null;
		Node ruleSet = null;
		Iterator fujaba__IterFigureToRule = null;
		Node rule = null;
		Iterator fujaba__IterFiguresToFigure = null;
		Node figure = null;

		// story pattern get Figure
		try {
			fujaba__Success = false;

			// check object in is really bound
			JavaSDM.ensure(in != null);
			// check object pack is really bound
			JavaSDM.ensure(pack != null);
			// iterate to-many link children from in to Figures
			fujaba__Success = false;

			fujaba__IterInToFigures = in.getChildren().iterator();

			while (!(fujaba__Success) && fujaba__IterInToFigures.hasNext()) {
				try {
					_TmpObject = fujaba__IterInToFigures.next();

					// ensure correct type and really bound of object Figures
					JavaSDM.ensure(_TmpObject instanceof Node);
					Figures = (Node) _TmpObject;

					// check isomorphic binding between objects in and Figures
					JavaSDM.ensure(!in.equals(Figures));

					// attribute condition
					JavaSDM.ensure(JavaSDM.stringCompare(Figures.getName(),
							"FIGURES") == 0);

					// iterate to-many link children from in to board
					fujaba__Success = false;

					fujaba__IterInToBoard = in.getChildren().iterator();

					while (!(fujaba__Success)
							&& fujaba__IterInToBoard.hasNext()) {
						try {
							_TmpObject = fujaba__IterInToBoard.next();

							// ensure correct type and really bound of object board
							JavaSDM.ensure(_TmpObject instanceof Node);
							board = (Node) _TmpObject;

							// check isomorphic binding between objects board and Figures
							JavaSDM.ensure(!board.equals(Figures));

							// check isomorphic binding between objects in and board
							JavaSDM.ensure(!in.equals(board));

							// attribute condition
							JavaSDM.ensure(JavaSDM.stringCompare(
									board.getName(), "board") == 0);

							// iterate to-many link children from board to nodeHeight
							fujaba__Success = false;

							fujaba__IterBoardToNodeHeight = board.getChildren()
									.iterator();

							while (!(fujaba__Success)
									&& fujaba__IterBoardToNodeHeight.hasNext()) {
								try {
									_TmpObject = fujaba__IterBoardToNodeHeight
											.next();

									// ensure correct type and really bound of object nodeHeight
									JavaSDM.ensure(_TmpObject instanceof Node);
									nodeHeight = (Node) _TmpObject;

									// check isomorphic binding between objects nodeHeight and Figures
									JavaSDM.ensure(!nodeHeight.equals(Figures));

									// check isomorphic binding between objects nodeHeight and board
									JavaSDM.ensure(!nodeHeight.equals(board));

									// check isomorphic binding between objects nodeHeight and in
									JavaSDM.ensure(!nodeHeight.equals(in));

									// attribute condition
									JavaSDM.ensure(JavaSDM.stringCompare(
											nodeHeight.getName(), "height") == 0);

									// iterate to-many link children from board to nodeWidth
									fujaba__Success = false;

									fujaba__IterBoardToNodeWidth = board
											.getChildren().iterator();

									while (!(fujaba__Success)
											&& fujaba__IterBoardToNodeWidth
													.hasNext()) {
										try {
											_TmpObject = fujaba__IterBoardToNodeWidth
													.next();

											// ensure correct type and really bound of object nodeWidth
											JavaSDM.ensure(_TmpObject instanceof Node);
											nodeWidth = (Node) _TmpObject;

											// check isomorphic binding between objects nodeWidth and Figures
											JavaSDM.ensure(!nodeWidth
													.equals(Figures));

											// check isomorphic binding between objects nodeWidth and board
											JavaSDM.ensure(!nodeWidth
													.equals(board));

											// check isomorphic binding between objects nodeWidth and in
											JavaSDM.ensure(!nodeWidth
													.equals(in));

											// check isomorphic binding between objects nodeWidth and nodeHeight
											JavaSDM.ensure(!nodeWidth
													.equals(nodeHeight));

											// iterate to-many link children from nodeHeight to height
											fujaba__Success = false;

											fujaba__IterNodeHeightToHeight = nodeHeight
													.getChildren().iterator();

											while (!(fujaba__Success)
													&& fujaba__IterNodeHeightToHeight
															.hasNext()) {
												try {
													_TmpObject = fujaba__IterNodeHeightToHeight
															.next();

													// ensure correct type and really bound of object height
													JavaSDM.ensure(_TmpObject instanceof Node);
													height = (Node) _TmpObject;

													// check isomorphic binding between objects height and Figures
													JavaSDM.ensure(!height
															.equals(Figures));

													// check isomorphic binding between objects height and board
													JavaSDM.ensure(!height
															.equals(board));

													// check isomorphic binding between objects in and height
													JavaSDM.ensure(!in
															.equals(height));

													// check isomorphic binding between objects nodeHeight and height
													JavaSDM.ensure(!nodeHeight
															.equals(height));

													// check isomorphic binding between objects nodeWidth and height
													JavaSDM.ensure(!nodeWidth
															.equals(height));

													// iterate to-many link children from nodeWidth to width
													fujaba__Success = false;

													fujaba__IterNodeWidthToWidth = nodeWidth
															.getChildren()
															.iterator();

													while (!(fujaba__Success)
															&& fujaba__IterNodeWidthToWidth
																	.hasNext()) {
														try {
															_TmpObject = fujaba__IterNodeWidthToWidth
																	.next();

															// ensure correct type and really bound of object width
															JavaSDM.ensure(_TmpObject instanceof Node);
															width = (Node) _TmpObject;

															// check isomorphic binding between objects width and Figures
															JavaSDM.ensure(!width
																	.equals(Figures));

															// check isomorphic binding between objects width and board
															JavaSDM.ensure(!width
																	.equals(board));

															// check isomorphic binding between objects width and height
															JavaSDM.ensure(!width
																	.equals(height));

															// check isomorphic binding between objects width and in
															JavaSDM.ensure(!width
																	.equals(in));

															// check isomorphic binding between objects width and nodeHeight
															JavaSDM.ensure(!width
																	.equals(nodeHeight));

															// check isomorphic binding between objects width and nodeWidth
															JavaSDM.ensure(!width
																	.equals(nodeWidth));

															fujaba__Success = true;
														} catch (JavaSDMException fujaba__InternalException) {
															fujaba__Success = false;
														}
													}
													JavaSDM.ensure(fujaba__Success);

													fujaba__Success = true;
												} catch (JavaSDMException fujaba__InternalException) {
													fujaba__Success = false;
												}
											}
											JavaSDM.ensure(fujaba__Success);
											// attribute condition
											JavaSDM.ensure(JavaSDM.stringCompare(
													nodeWidth.getName(),
													"width") == 0);

											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}
									}
									JavaSDM.ensure(fujaba__Success);

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}
							}
							JavaSDM.ensure(fujaba__Success);

							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}
					}
					JavaSDM.ensure(fujaba__Success);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			// iterate to-many link eClassifiers from pack to GameLogicClazz
			fujaba__Success = false;

			fujaba__IterPackToGameLogicClazz = pack.getEClassifiers()
					.iterator();

			while (!(fujaba__Success)
					&& fujaba__IterPackToGameLogicClazz.hasNext()) {
				try {
					_TmpObject = fujaba__IterPackToGameLogicClazz.next();

					// ensure correct type and really bound of object GameLogicClazz
					JavaSDM.ensure(_TmpObject instanceof EClass);
					GameLogicClazz = (EClass) _TmpObject;

					// attribute condition
					JavaSDM.ensure(JavaSDM.stringCompare(
							GameLogicClazz.getName(), "GameLogic") == 0);

					// iterate to-many link eAttributes from GameLogicClazz to attrHeight
					fujaba__Success = false;

					fujaba__IterGameLogicClazzToAttrHeight = GameLogicClazz
							.getEAttributes().iterator();

					while (!(fujaba__Success)
							&& fujaba__IterGameLogicClazzToAttrHeight.hasNext()) {
						try {
							attrHeight = (EAttribute) fujaba__IterGameLogicClazzToAttrHeight
									.next();

							// check object attrHeight is really bound
							JavaSDM.ensure(attrHeight != null);
							// attribute condition
							JavaSDM.ensure(JavaSDM.stringCompare(
									attrHeight.getName(), "height") == 0);

							// iterate to-many link eAttributes from GameLogicClazz to attrWidth
							fujaba__Success = false;

							fujaba__IterGameLogicClazzToAttrWidth = GameLogicClazz
									.getEAttributes().iterator();

							while (!(fujaba__Success)
									&& fujaba__IterGameLogicClazzToAttrWidth
											.hasNext()) {
								try {
									attrWidth = (EAttribute) fujaba__IterGameLogicClazzToAttrWidth
											.next();

									// check object attrWidth is really bound
									JavaSDM.ensure(attrWidth != null);
									// check isomorphic binding between objects attrWidth and attrHeight
									JavaSDM.ensure(!attrWidth
											.equals(attrHeight));

									// attribute condition
									JavaSDM.ensure(JavaSDM.stringCompare(
											attrWidth.getName(), "width") == 0);

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}
							}
							JavaSDM.ensure(fujaba__Success);

							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}
					}
					JavaSDM.ensure(fujaba__Success);
					// iterate to-many link eClassifiers from pack to fieldClazz
					fujaba__Success = false;

					fujaba__IterPackToFieldClazz = pack.getEClassifiers()
							.iterator();

					while (!(fujaba__Success)
							&& fujaba__IterPackToFieldClazz.hasNext()) {
						try {
							_TmpObject = fujaba__IterPackToFieldClazz.next();

							// ensure correct type and really bound of object fieldClazz
							JavaSDM.ensure(_TmpObject instanceof EClass);
							fieldClazz = (EClass) _TmpObject;

							// check isomorphic binding between objects fieldClazz and GameLogicClazz
							JavaSDM.ensure(!fieldClazz.equals(GameLogicClazz));

							// attribute condition
							JavaSDM.ensure(JavaSDM.stringCompare(
									fieldClazz.getName(), "Field") == 0);

							// iterate to-many link eClassifiers from pack to figClazz
							fujaba__Success = false;

							fujaba__IterPackToFigClazz = pack.getEClassifiers()
									.iterator();

							while (!(fujaba__Success)
									&& fujaba__IterPackToFigClazz.hasNext()) {
								try {
									_TmpObject = fujaba__IterPackToFigClazz
											.next();

									// ensure correct type and really bound of object figClazz
									JavaSDM.ensure(_TmpObject instanceof EClass);
									figClazz = (EClass) _TmpObject;

									// check isomorphic binding between objects figClazz and GameLogicClazz
									JavaSDM.ensure(!figClazz
											.equals(GameLogicClazz));

									// check isomorphic binding between objects figClazz and fieldClazz
									JavaSDM.ensure(!figClazz.equals(fieldClazz));

									// attribute condition
									JavaSDM.ensure(JavaSDM.stringCompare(
											figClazz.getName(), "Figure") == 0);

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}
							}
							JavaSDM.ensure(fujaba__Success);

							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}
					}
					JavaSDM.ensure(fujaba__Success);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			// assign attribute attrHeight
			attrHeight.setDefaultValueLiteral(height.getName());
			// assign attribute attrWidth
			attrWidth.setDefaultValueLiteral(width.getName());
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern for each figure
		try {
			fujaba__Success = false;

			// check object Figures is really bound
			JavaSDM.ensure(Figures != null);
			// iterate to-many link children from Figures to figure
			fujaba__Success = false;

			fujaba__IterFiguresToFigure = new ArrayList(Figures.getChildren())
					.iterator();

			while (fujaba__IterFiguresToFigure.hasNext()) {
				try {
					_TmpObject = fujaba__IterFiguresToFigure.next();

					// ensure correct type and really bound of object figure
					JavaSDM.ensure(_TmpObject instanceof Node);
					figure = (Node) _TmpObject;

					// check isomorphic binding between objects figure and Figures
					JavaSDM.ensure(!figure.equals(Figures));

					// create object eAnnotation
					eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
					// assign attribute eAnnotation
					eAnnotation.setSource("SDM");
					// story pattern create checkAndMove
					try {
						fujaba__Success = false;

						_TmpObject = (this.createCheckAndMoveTemplate(figure,
								pack, figClazz, eAnnotation));

						// ensure correct type and really bound of object activity
						JavaSDM.ensure(_TmpObject instanceof Activity);
						activity = (Activity) _TmpObject;

						// check object eAnnotation is really bound
						JavaSDM.ensure(eAnnotation != null);
						// check object figClazz is really bound
						JavaSDM.ensure(figClazz != null);
						// check object figure is really bound
						JavaSDM.ensure(figure != null);
						// check object pack is really bound
						JavaSDM.ensure(pack != null);
						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					// story pattern for each Rule
					try {
						fujaba__Success = false;

						// check object figure is really bound
						JavaSDM.ensure(figure != null);
						// iterate to-many link children from figure to rule
						fujaba__Success = false;

						fujaba__IterFigureToRule = new ArrayList(
								figure.getChildren()).iterator();

						while (fujaba__IterFigureToRule.hasNext()) {
							try {
								_TmpObject = fujaba__IterFigureToRule.next();

								// ensure correct type and really bound of object rule
								JavaSDM.ensure(_TmpObject instanceof Node);
								rule = (Node) _TmpObject;

								// check isomorphic binding between objects rule and figure
								JavaSDM.ensure(!rule.equals(figure));

								// iterate to-many link children from rule to ruleSet
								fujaba__Success = false;

								fujaba__IterRuleToRuleSet = new ArrayList(
										rule.getChildren()).iterator();

								while (fujaba__IterRuleToRuleSet.hasNext()) {
									try {
										_TmpObject = fujaba__IterRuleToRuleSet
												.next();

										// ensure correct type and really bound of object ruleSet
										JavaSDM.ensure(_TmpObject instanceof Node);
										ruleSet = (Node) _TmpObject;

										// check isomorphic binding between objects ruleSet and figure
										JavaSDM.ensure(!ruleSet.equals(figure));

										// check isomorphic binding between objects ruleSet and rule
										JavaSDM.ensure(!ruleSet.equals(rule));

										// attribute condition
										JavaSDM.ensure(JavaSDM.stringCompare(
												ruleSet.getName(), "SET") == 0);

										// attribute condition
										JavaSDM.ensure(JavaSDM.stringCompare(
												rule.getName(), "rule") == 0);

										// story pattern create Rule
										try {
											fujaba__Success = false;

											_TmpObject = (this.addRule(pack,
													activity));

											// ensure correct type and really bound of object ruleStory
											JavaSDM.ensure(_TmpObject instanceof StoryPattern);
											ruleStory = (StoryPattern) _TmpObject;

											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}

										// story pattern for each move
										try {
											fujaba__Success = false;

											// check object ruleSet is really bound
											JavaSDM.ensure(ruleSet != null);
											// iterate to-many link children from ruleSet to move
											fujaba__Success = false;

											fujaba__IterRuleSetToMove = new ArrayList(
													ruleSet.getChildren())
													.iterator();

											while (fujaba__IterRuleSetToMove
													.hasNext()) {
												try {
													_TmpObject = fujaba__IterRuleSetToMove
															.next();

													// ensure correct type and really bound of object move
													JavaSDM.ensure(_TmpObject instanceof Node);
													move = (Node) _TmpObject;

													// check isomorphic binding between objects ruleSet and move
													JavaSDM.ensure(!ruleSet
															.equals(move));

													// story pattern is Move
													try {
														fujaba__Success = false;

														// check object move is really bound
														JavaSDM.ensure(move != null);
														// check object pack is really bound
														JavaSDM.ensure(pack != null);
														// check object ruleStory is really bound
														JavaSDM.ensure(ruleStory != null);
														// attribute condition
														JavaSDM.ensure(JavaSDM.stringCompare(
																move.getName(),
																"destroy") != 0);

														// attribute condition
														JavaSDM.ensure(JavaSDM.stringCompare(
																move.getName(),
																"empty") != 0);

														// iterate to-many link objectVariable from ruleStory to lastMove
														fujaba__Success = false;

														fujaba__IterRuleStoryToLastMove = ruleStory
																.getObjectVariable()
																.iterator();

														while (!(fujaba__Success)
																&& fujaba__IterRuleStoryToLastMove
																		.hasNext()) {
															try {
																lastMove = (ObjectVariable) fujaba__IterRuleStoryToLastMove
																		.next();

																// check object lastMove is really bound
																JavaSDM.ensure(lastMove != null);
																// bind object
																_TmpObject = lastMove
																		.getType();

																// ensure correct type and really bound of object fieldClazz
																JavaSDM.ensure(_TmpObject instanceof EClass);
																fieldClazz = (EClass) _TmpObject;

																// check link eClassifiers from pack to fieldClazz
																JavaSDM.ensure(pack
																		.getEClassifiers()
																		.contains(
																				fieldClazz));

																// attribute condition
																JavaSDM.ensure(JavaSDM
																		.stringCompare(
																				fieldClazz
																						.getName(),
																				"Field") == 0);

																// check negative bindings
																try {
																	fujaba__Success = false;

																	// iterate to-many link source from lastMove to noLink
																	fujaba__Success = false;

																	fujaba__IterLastMoveToNoLink = lastMove
																			.getOutgoingLink()
																			.iterator();

																	while (!(fujaba__Success)
																			&& fujaba__IterLastMoveToNoLink
																					.hasNext()) {
																		try {
																			noLink = (LinkVariable) fujaba__IterLastMoveToNoLink
																					.next();

																			// check object noLink is really bound
																			JavaSDM.ensure(noLink != null);
																			// check link linkVariable from noLink to ruleStory
																			JavaSDM.ensure(ruleStory
																					.equals(noLink
																							.getPattern()));

																			// attribute condition
																			JavaSDM.ensure(noLink
																					.getBindingOperator() == BindingOperator.CHECK_ONLY);

																			// attribute condition
																			JavaSDM.ensure(noLink
																					.getBindingSemantics() != BindingSemantics.NEGATIVE);

																			// attribute condition
																			JavaSDM.ensure(JavaSDM
																					.stringCompare(
																							noLink.getName(),
																							"figure") != 0);

																			fujaba__Success = true;
																		} catch (JavaSDMException fujaba__InternalException) {
																			fujaba__Success = false;
																		}
																	}
																	JavaSDM.ensure(fujaba__Success);

																	fujaba__Success = true;
																} catch (JavaSDMException fujaba__InternalException) {
																	fujaba__Success = false;
																}

																fujaba__Success = !(fujaba__Success);

																JavaSDM.ensure(fujaba__Success);

																fujaba__Success = true;
															} catch (JavaSDMException fujaba__InternalException) {
																fujaba__Success = false;
															}
														}
														JavaSDM.ensure(fujaba__Success);
														// create object newMove
														newMove = PatternsFactory.eINSTANCE
																.createObjectVariable();
														// create object newLinkVar
														newLinkVar = PatternsFactory.eINSTANCE
																.createLinkVariable();
														// assign attribute newMove
														newMove.setName("fieldMove"
																+ move.getIndex());
														// assign attribute newMove
														newMove.setBindingOperator(BindingOperator.CHECK_ONLY);
														// assign attribute newMove
														newMove.setBindingSemantics(BindingSemantics.MANDATORY);
														// assign attribute newMove
														newMove.setBindingState(BindingState.UNBOUND);
														// assign attribute newLinkVar
														newLinkVar.setName(move
																.getName());
														// assign attribute newLinkVar
														newLinkVar
																.setBindingOperator(BindingOperator.CHECK_ONLY);
														// create link
														newMove.setPattern(ruleStory);

														// create link
														newLinkVar
																.setPattern(ruleStory);

														// create link
														newLinkVar
																.setSource(lastMove);

														// create link
														org.moflon.util.eMoflonEMFUtil
																.addOppositeReference(
																		newMove,
																		fieldClazz,
																		"type");
														// create link
														newLinkVar
																.setTarget(newMove);

														fujaba__Success = true;
													} catch (JavaSDMException fujaba__InternalException) {
														fujaba__Success = false;
													}

													if (fujaba__Success) {

													} else {
														// story pattern ActivityNode6
														try {
															fujaba__Success = false;

															// check object move is really bound
															JavaSDM.ensure(move != null);
															// check object pack is really bound
															JavaSDM.ensure(pack != null);
															// check object ruleStory is really bound
															JavaSDM.ensure(ruleStory != null);
															// attribute condition
															JavaSDM.ensure(JavaSDM
																	.stringCompare(
																			move.getName(),
																			"destroy") == 0);

															// iterate to-many link eClassifiers from pack to fieldClazz
															fujaba__Success = false;

															fujaba__IterPackToFieldClazz = pack
																	.getEClassifiers()
																	.iterator();

															while (!(fujaba__Success)
																	&& fujaba__IterPackToFieldClazz
																			.hasNext()) {
																try {
																	_TmpObject = fujaba__IterPackToFieldClazz
																			.next();

																	// ensure correct type and really bound of object fieldClazz
																	JavaSDM.ensure(_TmpObject instanceof EClass);
																	fieldClazz = (EClass) _TmpObject;

																	// attribute condition
																	JavaSDM.ensure(JavaSDM
																			.stringCompare(
																					fieldClazz
																							.getName(),
																					"Field") == 0);

																	// iterate to-many link eClassifiers from pack to figureClazz
																	fujaba__Success = false;

																	fujaba__IterPackToFigureClazz = pack
																			.getEClassifiers()
																			.iterator();

																	while (!(fujaba__Success)
																			&& fujaba__IterPackToFigureClazz
																					.hasNext()) {
																		try {
																			_TmpObject = fujaba__IterPackToFigureClazz
																					.next();

																			// ensure correct type and really bound of object figureClazz
																			JavaSDM.ensure(_TmpObject instanceof EClass);
																			figureClazz = (EClass) _TmpObject;

																			// check isomorphic binding between objects figureClazz and fieldClazz
																			JavaSDM.ensure(!figureClazz
																					.equals(fieldClazz));

																			// attribute condition
																			JavaSDM.ensure(JavaSDM
																					.stringCompare(
																							figureClazz
																									.getName(),
																							"Figure") == 0);

																			fujaba__Success = true;
																		} catch (JavaSDMException fujaba__InternalException) {
																			fujaba__Success = false;
																		}
																	}
																	JavaSDM.ensure(fujaba__Success);
																	// iterate to-many link objectVariable from ruleStory to lastMove
																	fujaba__Success = false;

																	fujaba__IterRuleStoryToLastMove = ruleStory
																			.getObjectVariable()
																			.iterator();

																	while (!(fujaba__Success)
																			&& fujaba__IterRuleStoryToLastMove
																					.hasNext()) {
																		try {
																			lastMove = (ObjectVariable) fujaba__IterRuleStoryToLastMove
																					.next();

																			// check object lastMove is really bound
																			JavaSDM.ensure(lastMove != null);
																			// check link type from lastMove to fieldClazz
																			JavaSDM.ensure(fieldClazz
																					.equals(lastMove
																							.getType()));

																			// check negative bindings
																			try {
																				fujaba__Success = false;

																				// iterate to-many link source from lastMove to noLink
																				fujaba__Success = false;

																				fujaba__IterLastMoveToNoLink = lastMove
																						.getOutgoingLink()
																						.iterator();

																				while (!(fujaba__Success)
																						&& fujaba__IterLastMoveToNoLink
																								.hasNext()) {
																					try {
																						noLink = (LinkVariable) fujaba__IterLastMoveToNoLink
																								.next();

																						// check object noLink is really bound
																						JavaSDM.ensure(noLink != null);
																						// check link linkVariable from noLink to ruleStory
																						JavaSDM.ensure(ruleStory
																								.equals(noLink
																										.getPattern()));

																						// attribute condition
																						JavaSDM.ensure(noLink
																								.getBindingOperator() == BindingOperator.CHECK_ONLY);

																						// attribute condition
																						JavaSDM.ensure(noLink
																								.getBindingSemantics() != BindingSemantics.NEGATIVE);

																						// attribute condition
																						JavaSDM.ensure(JavaSDM
																								.stringCompare(
																										noLink.getName(),
																										"figure") != 0);

																						fujaba__Success = true;
																					} catch (JavaSDMException fujaba__InternalException) {
																						fujaba__Success = false;
																					}
																				}
																				JavaSDM.ensure(fujaba__Success);

																				fujaba__Success = true;
																			} catch (JavaSDMException fujaba__InternalException) {
																				fujaba__Success = false;
																			}

																			fujaba__Success = !(fujaba__Success);

																			JavaSDM.ensure(fujaba__Success);

																			fujaba__Success = true;
																		} catch (JavaSDMException fujaba__InternalException) {
																			fujaba__Success = false;
																		}
																	}
																	JavaSDM.ensure(fujaba__Success);

																	fujaba__Success = true;
																} catch (JavaSDMException fujaba__InternalException) {
																	fujaba__Success = false;
																}
															}
															JavaSDM.ensure(fujaba__Success);
															// create object figureToDestroy
															figureToDestroy = PatternsFactory.eINSTANCE
																	.createObjectVariable();
															// create object newLinkVar
															newLinkVar = PatternsFactory.eINSTANCE
																	.createLinkVariable();
															// assign attribute figureToDestroy
															figureToDestroy
																	.setBindingOperator(BindingOperator.CHECK_ONLY);
															// assign attribute figureToDestroy
															figureToDestroy
																	.setBindingSemantics(BindingSemantics.MANDATORY);
															// assign attribute figureToDestroy
															figureToDestroy
																	.setBindingState(BindingState.UNBOUND);
															// assign attribute figureToDestroy
															figureToDestroy
																	.setName("figureToBeDestroyed"
																			+ move.getIndex());
															// assign attribute newLinkVar
															newLinkVar
																	.setBindingOperator(BindingOperator.DESTROY);
															// assign attribute newLinkVar
															newLinkVar
																	.setName("figure");
															// create link
															org.moflon.util.eMoflonEMFUtil
																	.addOppositeReference(
																			figureToDestroy,
																			figureClazz,
																			"type");
															// create link
															newLinkVar
																	.setTarget(figureToDestroy);

															// create link
															ruleStory
																	.getObjectVariable()
																	.add(figureToDestroy);

															// create link
															lastMove.getOutgoingLink()
																	.add(newLinkVar);

															// create link
															ruleStory
																	.getLinkVariable()
																	.add(newLinkVar);

															fujaba__Success = true;
														} catch (JavaSDMException fujaba__InternalException) {
															fujaba__Success = false;
														}

														if (fujaba__Success) {

														} else {
															// story pattern is Empty
															try {
																fujaba__Success = false;

																// check object pack is really bound
																JavaSDM.ensure(pack != null);
																// check object ruleStory is really bound
																JavaSDM.ensure(ruleStory != null);
																// attribute condition
																JavaSDM.ensure(JavaSDM
																		.stringCompare(
																				move.getName(),
																				"empty") == 0);

																// iterate to-many link eClassifiers from pack to figureClass
																fujaba__Success = false;

																fujaba__IterPackToFigureClass = pack
																		.getEClassifiers()
																		.iterator();

																while (!(fujaba__Success)
																		&& fujaba__IterPackToFigureClass
																				.hasNext()) {
																	try {
																		_TmpObject = fujaba__IterPackToFigureClass
																				.next();

																		// ensure correct type and really bound of object figureClass
																		JavaSDM.ensure(_TmpObject instanceof EClass);
																		figureClass = (EClass) _TmpObject;

																		// attribute condition
																		JavaSDM.ensure(JavaSDM
																				.stringCompare(
																						figureClass
																								.getName(),
																						"Figure") == 0);

																		// iterate to-many link objectVariable from ruleStory to lastMove
																		fujaba__Success = false;

																		fujaba__IterRuleStoryToLastMove = ruleStory
																				.getObjectVariable()
																				.iterator();

																		while (!(fujaba__Success)
																				&& fujaba__IterRuleStoryToLastMove
																						.hasNext()) {
																			try {
																				lastMove = (ObjectVariable) fujaba__IterRuleStoryToLastMove
																						.next();

																				// check object lastMove is really bound
																				JavaSDM.ensure(lastMove != null);
																				// bind object
																				_TmpObject = lastMove
																						.getType();

																				// ensure correct type and really bound of object fieldClazz
																				JavaSDM.ensure(_TmpObject instanceof EClass);
																				fieldClazz = (EClass) _TmpObject;

																				// check isomorphic binding between objects figureClass and fieldClazz
																				JavaSDM.ensure(!figureClass
																						.equals(fieldClazz));

																				// check link eClassifiers from fieldClazz to pack
																				JavaSDM.ensure(org.moflon.util.eMoflonEMFUtil
																						.getOppositeReference(
																								fieldClazz,
																								EPackage.class,
																								"eClassifiers")
																						.contains(
																								pack));

																				// attribute condition
																				JavaSDM.ensure(JavaSDM
																						.stringCompare(
																								fieldClazz
																										.getName(),
																								"Field") == 0);

																				// check negative bindings
																				try {
																					fujaba__Success = false;

																					// iterate to-many link source from lastMove to noLink
																					fujaba__Success = false;

																					fujaba__IterLastMoveToNoLink = lastMove
																							.getOutgoingLink()
																							.iterator();

																					while (!(fujaba__Success)
																							&& fujaba__IterLastMoveToNoLink
																									.hasNext()) {
																						try {
																							noLink = (LinkVariable) fujaba__IterLastMoveToNoLink
																									.next();

																							// check object noLink is really bound
																							JavaSDM.ensure(noLink != null);
																							// check link linkVariable from noLink to ruleStory
																							JavaSDM.ensure(ruleStory
																									.equals(noLink
																											.getPattern()));

																							// attribute condition
																							JavaSDM.ensure(noLink
																									.getBindingOperator() == BindingOperator.CHECK_ONLY);

																							// attribute condition
																							JavaSDM.ensure(noLink
																									.getBindingSemantics() != BindingSemantics.NEGATIVE);

																							// attribute condition
																							JavaSDM.ensure(JavaSDM
																									.stringCompare(
																											noLink.getName(),
																											"figure") != 0);

																							fujaba__Success = true;
																						} catch (JavaSDMException fujaba__InternalException) {
																							fujaba__Success = false;
																						}
																					}
																					JavaSDM.ensure(fujaba__Success);

																					fujaba__Success = true;
																				} catch (JavaSDMException fujaba__InternalException) {
																					fujaba__Success = false;
																				}

																				fujaba__Success = !(fujaba__Success);

																				JavaSDM.ensure(fujaba__Success);

																				fujaba__Success = true;
																			} catch (JavaSDMException fujaba__InternalException) {
																				fujaba__Success = false;
																			}
																		}
																		JavaSDM.ensure(fujaba__Success);

																		fujaba__Success = true;
																	} catch (JavaSDMException fujaba__InternalException) {
																		fujaba__Success = false;
																	}
																}
																JavaSDM.ensure(fujaba__Success);
																// create object newLinkVar
																newLinkVar = PatternsFactory.eINSTANCE
																		.createLinkVariable();
																// create object noFigure
																noFigure = PatternsFactory.eINSTANCE
																		.createObjectVariable();
																// assign attribute noFigure
																noFigure.setBindingOperator(BindingOperator.CHECK_ONLY);
																// assign attribute noFigure
																noFigure.setBindingSemantics(BindingSemantics.NEGATIVE);
																// assign attribute noFigure
																noFigure.setBindingState(BindingState.UNBOUND);
																// assign attribute noFigure
																noFigure.setName("noFigure"
																		+ move.getIndex());
																// assign attribute newLinkVar
																newLinkVar
																		.setBindingOperator(BindingOperator.CHECK_ONLY);
																// assign attribute newLinkVar
																newLinkVar
																		.setName("figure");
																// assign attribute newLinkVar
																newLinkVar
																		.setBindingSemantics(BindingSemantics.MANDATORY);
																// create link
																newLinkVar
																		.setPattern(ruleStory);

																// create link
																noFigure.setPattern(ruleStory);

																// create link
																newLinkVar
																		.setSource(lastMove);

																// create link
																org.moflon.util.eMoflonEMFUtil
																		.addOppositeReference(
																				noFigure,
																				figureClass,
																				"type");
																// create link
																newLinkVar
																		.setTarget(noFigure);

																fujaba__Success = true;
															} catch (JavaSDMException fujaba__InternalException) {
																fujaba__Success = false;
															}

														}

													}

													fujaba__Success = true;
												} catch (JavaSDMException fujaba__InternalException) {
													fujaba__Success = false;
												}
											}
											JavaSDM.ensure(fujaba__Success);
											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}

										// story pattern add pressed field check
										try {
											fujaba__Success = false;

											// check object pack is really bound
											JavaSDM.ensure(pack != null);
											// check object ruleStory is really bound
											JavaSDM.ensure(ruleStory != null);
											// iterate to-many link eClassifiers from pack to fieldClazz
											fujaba__Success = false;

											fujaba__IterPackToFieldClazz = pack
													.getEClassifiers()
													.iterator();

											while (!(fujaba__Success)
													&& fujaba__IterPackToFieldClazz
															.hasNext()) {
												try {
													_TmpObject = fujaba__IterPackToFieldClazz
															.next();

													// ensure correct type and really bound of object fieldClazz
													JavaSDM.ensure(_TmpObject instanceof EClass);
													fieldClazz = (EClass) _TmpObject;

													// attribute condition
													JavaSDM.ensure(JavaSDM.stringCompare(
															fieldClazz
																	.getName(),
															"Field") == 0);

													// iterate to-many link objectVariable from ruleStory to lastMove
													fujaba__Success = false;

													fujaba__IterRuleStoryToLastMove = ruleStory
															.getObjectVariable()
															.iterator();

													while (!(fujaba__Success)
															&& fujaba__IterRuleStoryToLastMove
																	.hasNext()) {
														try {
															lastMove = (ObjectVariable) fujaba__IterRuleStoryToLastMove
																	.next();

															// check object lastMove is really bound
															JavaSDM.ensure(lastMove != null);
															// check link type from lastMove to fieldClazz
															JavaSDM.ensure(fieldClazz
																	.equals(lastMove
																			.getType()));

															// check negative bindings
															try {
																fujaba__Success = false;

																// iterate to-many link source from lastMove to noLink
																fujaba__Success = false;

																fujaba__IterLastMoveToNoLink = lastMove
																		.getOutgoingLink()
																		.iterator();

																while (!(fujaba__Success)
																		&& fujaba__IterLastMoveToNoLink
																				.hasNext()) {
																	try {
																		noLink = (LinkVariable) fujaba__IterLastMoveToNoLink
																				.next();

																		// check object noLink is really bound
																		JavaSDM.ensure(noLink != null);
																		// check link linkVariable from noLink to ruleStory
																		JavaSDM.ensure(ruleStory
																				.equals(noLink
																						.getPattern()));

																		// attribute condition
																		JavaSDM.ensure(noLink
																				.getBindingOperator() == BindingOperator.CHECK_ONLY);

																		// attribute condition
																		JavaSDM.ensure(noLink
																				.getBindingSemantics() != BindingSemantics.NEGATIVE);

																		// attribute condition
																		JavaSDM.ensure(JavaSDM
																				.stringCompare(
																						noLink.getName(),
																						"figure") != 0);

																		fujaba__Success = true;
																	} catch (JavaSDMException fujaba__InternalException) {
																		fujaba__Success = false;
																	}
																}
																JavaSDM.ensure(fujaba__Success);

																fujaba__Success = true;
															} catch (JavaSDMException fujaba__InternalException) {
																fujaba__Success = false;
															}

															fujaba__Success = !(fujaba__Success);

															JavaSDM.ensure(fujaba__Success);

															fujaba__Success = true;
														} catch (JavaSDMException fujaba__InternalException) {
															fujaba__Success = false;
														}
													}
													JavaSDM.ensure(fujaba__Success);

													fujaba__Success = true;
												} catch (JavaSDMException fujaba__InternalException) {
													fujaba__Success = false;
												}
											}
											JavaSDM.ensure(fujaba__Success);
											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}

										// story pattern rename last Move
										try {
											fujaba__Success = false;

											// check object lastMove is really bound
											JavaSDM.ensure(lastMove != null);
											// assign attribute lastMove
											lastMove.setName("field");
											// assign attribute lastMove
											lastMove.setBindingState(BindingState.BOUND);
											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}

										fujaba__Success = true;
									} catch (JavaSDMException fujaba__InternalException) {
										fujaba__Success = false;
									}
								}
								JavaSDM.ensure(fujaba__Success);

								fujaba__Success = true;
							} catch (JavaSDMException fujaba__InternalException) {
								fujaba__Success = false;
							}
						}
						JavaSDM.ensure(fujaba__Success);
						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					this.addEnding(pack, activity);
					this.embedSDMInEAnnotation(activity, eAnnotation);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void embedSDMInEAnnotation(Activity sdm, EAnnotation eAnnotation) {
		ResourceSet resourceSet = new ResourceSetImpl();
		Resource resource = resourceSet.createResource(URI.createURI(""));
		resource.getContents().add(sdm);

		StringWriter writer = new StringWriter();
		OutputStream out = new URIConverter.WriteableOutputStream(writer,
				"UTF-8");
		try {
			resource.save(out, null);
		} catch (IOException e) {
			e.printStackTrace();
		}

		eAnnotation.getDetails().put("XMI", writer.toString());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Activity createCheckAndMoveTemplate(Node figureNode,
			EPackage ePackage, EClass figureClazz, EAnnotation eAnnotation) {
		boolean fujaba__Success = false;
		Iterator fujaba__IterEPackageToField = null;
		Object _TmpObject = null;
		EClass field = null;
		EClass newFigure = null;
		EOperation checkAndMove = null;
		EParameter paraField = null;
		Activity activity = null;
		StopNode stopNode = null;
		StartNode startNode = null;

		// story pattern createClassAndOperation
		try {
			fujaba__Success = false;

			// check object eAnnotation is really bound
			JavaSDM.ensure(eAnnotation != null);
			// check object ePackage is really bound
			JavaSDM.ensure(ePackage != null);
			// check object figureClazz is really bound
			JavaSDM.ensure(figureClazz != null);
			// check object figureNode is really bound
			JavaSDM.ensure(figureNode != null);
			// iterate to-many link eClassifiers from ePackage to field
			fujaba__Success = false;

			fujaba__IterEPackageToField = ePackage.getEClassifiers().iterator();

			while (!(fujaba__Success) && fujaba__IterEPackageToField.hasNext()) {
				try {
					_TmpObject = fujaba__IterEPackageToField.next();

					// ensure correct type and really bound of object field
					JavaSDM.ensure(_TmpObject instanceof EClass);
					field = (EClass) _TmpObject;

					// check isomorphic binding between objects figureClazz and field
					JavaSDM.ensure(!figureClazz.equals(field));

					// attribute condition
					JavaSDM.ensure(JavaSDM.stringCompare(field.getName(),
							"Field") == 0);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			// create object newFigure
			newFigure = EcoreFactory.eINSTANCE.createEClass();
			// create object checkAndMove
			checkAndMove = EcoreFactory.eINSTANCE.createEOperation();
			// create object paraField
			paraField = EcoreFactory.eINSTANCE.createEParameter();
			// create object activity
			activity = ActivitiesFactory.eINSTANCE.createActivity();
			// create object stopNode
			stopNode = ActivitiesFactory.eINSTANCE.createStopNode();
			// create object startNode
			startNode = ActivitiesFactory.eINSTANCE.createStartNode();
			// assign attribute newFigure
			newFigure.setName("Figure" + figureNode.getIndex());
			// assign attribute checkAndMove
			checkAndMove.setName("checkAndMove");
			// assign attribute paraField
			paraField.setName("field");
			// create link
			stopNode.setOwningActivity(activity);

			// create link
			startNode.setOwningActivity(activity);

			// create link
			org.moflon.util.eMoflonEMFUtil.addOppositeReference(ePackage,
					newFigure, "eClassifiers");
			// create link
			newFigure.getESuperTypes().add(figureClazz);

			// create link
			org.moflon.util.eMoflonEMFUtil.addOppositeReference(newFigure,
					checkAndMove, "eOperations");
			// create link
			org.moflon.util.eMoflonEMFUtil.addOppositeReference(checkAndMove,
					paraField, "eParameters");
			// create link
			eAnnotation.setEModelElement(checkAndMove);

			// create link
			activity.setOwningOperation(checkAndMove);

			// create link
			paraField.setEType(field);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return activity;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public StoryPattern addRule(EPackage pack, Activity activity) {
		boolean fujaba__Success = false;
		Iterator fujaba__IterPackToFigClazz = null;
		Object _TmpObject = null;
		EClass figClazz = null;
		Iterator fujaba__IterPackToFieldClazz = null;
		EClass fieldClazz = null;
		StoryNode newStoryNode = null;
		StoryPattern newStoryPattern = null;
		ObjectVariable thisFigure = null;
		ObjectVariable currentField = null;
		LinkVariable thisTocurrentField = null;
		Iterator fujaba__IterOldRuleToNoOutgoingEdge = null;
		ActivityEdge noOutgoingEdge = null;
		Iterator fujaba__IterActivityToOldRule = null;
		StoryNode oldRule = null;
		ActivityEdge oldTonew = null;
		Iterator fujaba__IterActivityToStartNode = null;
		StartNode startNode = null;
		ActivityEdge startToRule = null;

		// story pattern createRule
		try {
			fujaba__Success = false;

			// check object activity is really bound
			JavaSDM.ensure(activity != null);
			// check object pack is really bound
			JavaSDM.ensure(pack != null);
			// iterate to-many link eClassifiers from pack to fieldClazz
			fujaba__Success = false;

			fujaba__IterPackToFieldClazz = pack.getEClassifiers().iterator();

			while (!(fujaba__Success) && fujaba__IterPackToFieldClazz.hasNext()) {
				try {
					_TmpObject = fujaba__IterPackToFieldClazz.next();

					// ensure correct type and really bound of object fieldClazz
					JavaSDM.ensure(_TmpObject instanceof EClass);
					fieldClazz = (EClass) _TmpObject;

					// attribute condition
					JavaSDM.ensure(JavaSDM.stringCompare(fieldClazz.getName(),
							"Field") == 0);

					// iterate to-many link eClassifiers from pack to figClazz
					fujaba__Success = false;

					fujaba__IterPackToFigClazz = pack.getEClassifiers()
							.iterator();

					while (!(fujaba__Success)
							&& fujaba__IterPackToFigClazz.hasNext()) {
						try {
							_TmpObject = fujaba__IterPackToFigClazz.next();

							// ensure correct type and really bound of object figClazz
							JavaSDM.ensure(_TmpObject instanceof EClass);
							figClazz = (EClass) _TmpObject;

							// check isomorphic binding between objects figClazz and fieldClazz
							JavaSDM.ensure(!figClazz.equals(fieldClazz));

							// attribute condition
							JavaSDM.ensure(JavaSDM.stringCompare(
									figClazz.getName(), "Figure") == 0);

							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}
					}
					JavaSDM.ensure(fujaba__Success);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			// create object newStoryNode
			newStoryNode = ActivitiesFactory.eINSTANCE.createStoryNode();
			// create object newStoryPattern
			newStoryPattern = PatternsFactory.eINSTANCE.createStoryPattern();
			// create object thisFigure
			thisFigure = PatternsFactory.eINSTANCE.createObjectVariable();
			// create object currentField
			currentField = PatternsFactory.eINSTANCE.createObjectVariable();
			// create object thisTocurrentField
			thisTocurrentField = PatternsFactory.eINSTANCE.createLinkVariable();
			// assign attribute newStoryNode
			newStoryNode.setName("Rule Story");
			// assign attribute thisFigure
			thisFigure.setName("this");
			// assign attribute thisFigure
			thisFigure.setBindingOperator(BindingOperator.CHECK_ONLY);
			// assign attribute thisFigure
			thisFigure.setBindingSemantics(BindingSemantics.MANDATORY);
			// assign attribute thisFigure
			thisFigure.setBindingState(BindingState.BOUND);
			// assign attribute currentField
			currentField.setBindingOperator(BindingOperator.CHECK_ONLY);
			// assign attribute currentField
			currentField.setBindingSemantics(BindingSemantics.MANDATORY);
			// assign attribute currentField
			currentField.setBindingState(BindingState.UNBOUND);
			// assign attribute currentField
			currentField.setName("currentField");
			// assign attribute thisTocurrentField
			thisTocurrentField.setBindingOperator(BindingOperator.CHECK_ONLY);
			// assign attribute thisTocurrentField
			thisTocurrentField.setName("field");
			// create link
			newStoryPattern.setStoryNode(newStoryNode);

			// create link
			activity.getOwnedActivityNode().add(newStoryNode);

			// create link
			thisFigure.setPattern(newStoryPattern);

			// create link
			currentField.setPattern(newStoryPattern);

			// create link
			thisTocurrentField.setPattern(newStoryPattern);

			// create link
			org.moflon.util.eMoflonEMFUtil.addOppositeReference(thisFigure,
					figClazz, "type");
			// create link
			thisTocurrentField.setSource(thisFigure);

			// create link
			org.moflon.util.eMoflonEMFUtil.addOppositeReference(currentField,
					fieldClazz, "type");
			// create link
			thisTocurrentField.setTarget(currentField);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern findOldRule
		try {
			fujaba__Success = false;

			// check object activity is really bound
			JavaSDM.ensure(activity != null);
			// check object newStoryNode is really bound
			JavaSDM.ensure(newStoryNode != null);
			// iterate to-many link ownedActivityNode from activity to oldRule
			fujaba__Success = false;

			fujaba__IterActivityToOldRule = activity.getOwnedActivityNode()
					.iterator();

			while (!(fujaba__Success)
					&& fujaba__IterActivityToOldRule.hasNext()) {
				try {
					_TmpObject = fujaba__IterActivityToOldRule.next();

					// ensure correct type and really bound of object oldRule
					JavaSDM.ensure(_TmpObject instanceof StoryNode);
					oldRule = (StoryNode) _TmpObject;

					// check isomorphic binding between objects oldRule and newStoryNode
					JavaSDM.ensure(!oldRule.equals(newStoryNode));

					// check negative bindings
					try {
						fujaba__Success = false;

						// iterate to-many link source from oldRule to noOutgoingEdge
						fujaba__Success = false;

						fujaba__IterOldRuleToNoOutgoingEdge = oldRule
								.getOutgoing().iterator();

						while (!(fujaba__Success)
								&& fujaba__IterOldRuleToNoOutgoingEdge
										.hasNext()) {
							try {
								noOutgoingEdge = (ActivityEdge) fujaba__IterOldRuleToNoOutgoingEdge
										.next();

								// check object noOutgoingEdge is really bound
								JavaSDM.ensure(noOutgoingEdge != null);
								// check link ownedActivityEdge from noOutgoingEdge to activity
								JavaSDM.ensure(activity.equals(noOutgoingEdge
										.getOwningActivity()));

								fujaba__Success = true;
							} catch (JavaSDMException fujaba__InternalException) {
								fujaba__Success = false;
							}
						}
						JavaSDM.ensure(fujaba__Success);

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					fujaba__Success = !(fujaba__Success);

					JavaSDM.ensure(fujaba__Success);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		if (fujaba__Success) {
			// story pattern AttachRuleToOldRule
			try {
				fujaba__Success = false;

				// check object activity is really bound
				JavaSDM.ensure(activity != null);
				// check object newStoryNode is really bound
				JavaSDM.ensure(newStoryNode != null);
				// check object oldRule is really bound
				JavaSDM.ensure(oldRule != null);
				// check isomorphic binding between objects oldRule and newStoryNode
				JavaSDM.ensure(!oldRule.equals(newStoryNode));

				// create object oldTonew
				oldTonew = ActivitiesFactory.eINSTANCE.createActivityEdge();
				// assign attribute oldTonew
				oldTonew.setGuard(EdgeGuard.FAILURE);
				// create link
				oldTonew.setOwningActivity(activity);

				// create link
				oldTonew.setSource(oldRule);

				// create link
				newStoryNode.getIncoming().add(oldTonew);

				fujaba__Success = true;
			} catch (JavaSDMException fujaba__InternalException) {
				fujaba__Success = false;
			}

			return newStoryPattern;

		} else {
			// story pattern attachRuleToStart
			try {
				fujaba__Success = false;

				// check object activity is really bound
				JavaSDM.ensure(activity != null);
				// check object newStoryNode is really bound
				JavaSDM.ensure(newStoryNode != null);
				// iterate to-many link ownedActivityNode from activity to startNode
				fujaba__Success = false;

				fujaba__IterActivityToStartNode = activity
						.getOwnedActivityNode().iterator();

				while (!(fujaba__Success)
						&& fujaba__IterActivityToStartNode.hasNext()) {
					try {
						_TmpObject = fujaba__IterActivityToStartNode.next();

						// ensure correct type and really bound of object startNode
						JavaSDM.ensure(_TmpObject instanceof StartNode);
						startNode = (StartNode) _TmpObject;

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}
				}
				JavaSDM.ensure(fujaba__Success);
				// create object startToRule
				startToRule = ActivitiesFactory.eINSTANCE.createActivityEdge();
				// create link
				startToRule.setTarget(newStoryNode);

				// create link
				startToRule.setOwningActivity(activity);

				// create link
				startNode.getOutgoing().add(startToRule);

				fujaba__Success = true;
			} catch (JavaSDMException fujaba__InternalException) {
				fujaba__Success = false;
			}

			return newStoryPattern;

		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void addEnding(EPackage pack, Activity activity) {
		boolean fujaba__Success = false;
		Iterator fujaba__IterActivityToNoStory = null;
		Object _TmpObject = null;
		StoryNode noStory = null;
		Iterator fujaba__IterActivityToStartNode = null;
		StartNode startNode = null;
		Iterator fujaba__IterActivityToStopNode = null;
		StopNode stopNode = null;
		ActivityEdge startToStop = null;
		Iterator fujaba__IterPackToFigClazz = null;
		EClass figClazz = null;
		Iterator fujaba__IterPackToFieldClazz = null;
		EClass fieldClazz = null;
		ActivityEdge ruleToMove = null;
		StoryNode moveStoryNode = null;
		ActivityEdge moveToEnd = null;
		StoryPattern moveStoryPattern = null;
		ObjectVariable figureVar = null;
		ObjectVariable fieldVar = null;
		LinkVariable link = null;
		Iterator fujaba__IterActivityToRuleNode = null;
		StoryNode ruleNode = null;
		Iterator fujaba__IterLastRuleNodeToNotOnFailureEdge = null;
		ActivityEdge notOnFailureEdge = null;
		Iterator fujaba__IterActivityToLastRuleNode = null;
		StoryNode lastRuleNode = null;
		ActivityEdge lastToStop = null;

		// story pattern story?
		try {
			fujaba__Success = false;

			// check object activity is really bound
			JavaSDM.ensure(activity != null);
			// check negative bindings
			try {
				fujaba__Success = false;

				// iterate to-many link ownedActivityNode from activity to noStory
				fujaba__Success = false;

				fujaba__IterActivityToNoStory = activity.getOwnedActivityNode()
						.iterator();

				while (!(fujaba__Success)
						&& fujaba__IterActivityToNoStory.hasNext()) {
					try {
						_TmpObject = fujaba__IterActivityToNoStory.next();

						// ensure correct type and really bound of object noStory
						JavaSDM.ensure(_TmpObject instanceof StoryNode);
						noStory = (StoryNode) _TmpObject;

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}
				}
				JavaSDM.ensure(fujaba__Success);

				fujaba__Success = true;
			} catch (JavaSDMException fujaba__InternalException) {
				fujaba__Success = false;
			}

			fujaba__Success = !(fujaba__Success);

			JavaSDM.ensure(fujaba__Success);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		if (fujaba__Success) {
			// story pattern createEmptyActivity
			try {
				fujaba__Success = false;

				// check object activity is really bound
				JavaSDM.ensure(activity != null);
				// iterate to-many link ownedActivityNode from activity to startNode
				fujaba__Success = false;

				fujaba__IterActivityToStartNode = activity
						.getOwnedActivityNode().iterator();

				while (!(fujaba__Success)
						&& fujaba__IterActivityToStartNode.hasNext()) {
					try {
						_TmpObject = fujaba__IterActivityToStartNode.next();

						// ensure correct type and really bound of object startNode
						JavaSDM.ensure(_TmpObject instanceof StartNode);
						startNode = (StartNode) _TmpObject;

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}
				}
				JavaSDM.ensure(fujaba__Success);
				// iterate to-many link ownedActivityNode from activity to stopNode
				fujaba__Success = false;

				fujaba__IterActivityToStopNode = activity
						.getOwnedActivityNode().iterator();

				while (!(fujaba__Success)
						&& fujaba__IterActivityToStopNode.hasNext()) {
					try {
						_TmpObject = fujaba__IterActivityToStopNode.next();

						// ensure correct type and really bound of object stopNode
						JavaSDM.ensure(_TmpObject instanceof StopNode);
						stopNode = (StopNode) _TmpObject;

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}
				}
				JavaSDM.ensure(fujaba__Success);
				// create object startToStop
				startToStop = ActivitiesFactory.eINSTANCE.createActivityEdge();
				// create link
				startToStop.setOwningActivity(activity);

				// create link
				startToStop.setSource(startNode);

				// create link
				startToStop.setTarget(stopNode);

				fujaba__Success = true;
			} catch (JavaSDMException fujaba__InternalException) {
				fujaba__Success = false;
			}

			return;

		} else {
			// story pattern ForEveryRule
			try {
				fujaba__Success = false;

				// check object activity is really bound
				JavaSDM.ensure(activity != null);
				// iterate to-many link ownedActivityNode from activity to ruleNode
				fujaba__Success = false;

				fujaba__IterActivityToRuleNode = new ArrayList(
						activity.getOwnedActivityNode()).iterator();

				while (fujaba__IterActivityToRuleNode.hasNext()) {
					try {
						_TmpObject = fujaba__IterActivityToRuleNode.next();

						// ensure correct type and really bound of object ruleNode
						JavaSDM.ensure(_TmpObject instanceof StoryNode);
						ruleNode = (StoryNode) _TmpObject;

						// story pattern MakeMove
						try {
							fujaba__Success = false;

							// check object activity is really bound
							JavaSDM.ensure(activity != null);
							// check object pack is really bound
							JavaSDM.ensure(pack != null);
							// check object ruleNode is really bound
							JavaSDM.ensure(ruleNode != null);
							// iterate to-many link ownedActivityNode from activity to stopNode
							fujaba__Success = false;

							fujaba__IterActivityToStopNode = activity
									.getOwnedActivityNode().iterator();

							while (!(fujaba__Success)
									&& fujaba__IterActivityToStopNode.hasNext()) {
								try {
									_TmpObject = fujaba__IterActivityToStopNode
											.next();

									// ensure correct type and really bound of object stopNode
									JavaSDM.ensure(_TmpObject instanceof StopNode);
									stopNode = (StopNode) _TmpObject;

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}
							}
							JavaSDM.ensure(fujaba__Success);
							// iterate to-many link eClassifiers from pack to fieldClazz
							fujaba__Success = false;

							fujaba__IterPackToFieldClazz = pack
									.getEClassifiers().iterator();

							while (!(fujaba__Success)
									&& fujaba__IterPackToFieldClazz.hasNext()) {
								try {
									_TmpObject = fujaba__IterPackToFieldClazz
											.next();

									// ensure correct type and really bound of object fieldClazz
									JavaSDM.ensure(_TmpObject instanceof EClass);
									fieldClazz = (EClass) _TmpObject;

									// attribute condition
									JavaSDM.ensure(JavaSDM.stringCompare(
											fieldClazz.getName(), "Field") == 0);

									// iterate to-many link eClassifiers from pack to figClazz
									fujaba__Success = false;

									fujaba__IterPackToFigClazz = pack
											.getEClassifiers().iterator();

									while (!(fujaba__Success)
											&& fujaba__IterPackToFigClazz
													.hasNext()) {
										try {
											_TmpObject = fujaba__IterPackToFigClazz
													.next();

											// ensure correct type and really bound of object figClazz
											JavaSDM.ensure(_TmpObject instanceof EClass);
											figClazz = (EClass) _TmpObject;

											// check isomorphic binding between objects figClazz and fieldClazz
											JavaSDM.ensure(!figClazz
													.equals(fieldClazz));

											// attribute condition
											JavaSDM.ensure(JavaSDM
													.stringCompare(
															figClazz.getName(),
															"Figure") == 0);

											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}
									}
									JavaSDM.ensure(fujaba__Success);

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}
							}
							JavaSDM.ensure(fujaba__Success);
							// create object ruleToMove
							ruleToMove = ActivitiesFactory.eINSTANCE
									.createActivityEdge();
							// create object moveStoryNode
							moveStoryNode = ActivitiesFactory.eINSTANCE
									.createStoryNode();
							// create object moveToEnd
							moveToEnd = ActivitiesFactory.eINSTANCE
									.createActivityEdge();
							// create object moveStoryPattern
							moveStoryPattern = PatternsFactory.eINSTANCE
									.createStoryPattern();
							// create object figureVar
							figureVar = PatternsFactory.eINSTANCE
									.createObjectVariable();
							// create object fieldVar
							fieldVar = PatternsFactory.eINSTANCE
									.createObjectVariable();
							// create object link
							link = PatternsFactory.eINSTANCE
									.createLinkVariable();
							// assign attribute ruleToMove
							ruleToMove.setGuard(EdgeGuard.SUCCESS);
							// assign attribute figureVar
							figureVar.setName("this");
							// assign attribute figureVar
							figureVar
									.setBindingOperator(BindingOperator.CHECK_ONLY);
							// assign attribute figureVar
							figureVar
									.setBindingSemantics(BindingSemantics.MANDATORY);
							// assign attribute figureVar
							figureVar.setBindingState(BindingState.BOUND);
							// assign attribute link
							link.setBindingOperator(BindingOperator.CREATE);
							// assign attribute link
							link.setName("field");
							// assign attribute fieldVar
							fieldVar.setBindingOperator(BindingOperator.CHECK_ONLY);
							// assign attribute fieldVar
							fieldVar.setBindingSemantics(BindingSemantics.MANDATORY);
							// assign attribute fieldVar
							fieldVar.setBindingState(BindingState.BOUND);
							// assign attribute fieldVar
							fieldVar.setName("field");
							// create link
							moveToEnd.setTarget(stopNode);

							// create link
							ruleToMove.setSource(ruleNode);

							// create link
							moveStoryNode.getIncoming().add(ruleToMove);

							// create link
							activity.getOwnedActivityEdge().add(ruleToMove);

							// create link
							moveToEnd.setOwningActivity(activity);

							// create link
							moveStoryNode.setOwningActivity(activity);

							// create link
							moveStoryPattern.setStoryNode(moveStoryNode);

							// create link
							moveToEnd.setSource(moveStoryNode);

							// create link
							figureVar.setPattern(moveStoryPattern);

							// create link
							fieldVar.setPattern(moveStoryPattern);

							// create link
							link.setPattern(moveStoryPattern);

							// create link
							org.moflon.util.eMoflonEMFUtil
									.addOppositeReference(figureVar, figClazz,
											"type");
							// create link
							link.setSource(figureVar);

							// create link
							fieldVar.getIncomingLink().add(link);

							// create link
							org.moflon.util.eMoflonEMFUtil
									.addOppositeReference(fieldVar, fieldClazz,
											"type");
							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}
				}
				JavaSDM.ensure(fujaba__Success);
				fujaba__Success = true;
			} catch (JavaSDMException fujaba__InternalException) {
				fujaba__Success = false;
			}

			// story pattern ConnectLastRuleWithStop
			try {
				fujaba__Success = false;

				// check object activity is really bound
				JavaSDM.ensure(activity != null);
				// iterate to-many link ownedActivityNode from activity to stopNode
				fujaba__Success = false;

				fujaba__IterActivityToStopNode = activity
						.getOwnedActivityNode().iterator();

				while (!(fujaba__Success)
						&& fujaba__IterActivityToStopNode.hasNext()) {
					try {
						_TmpObject = fujaba__IterActivityToStopNode.next();

						// ensure correct type and really bound of object stopNode
						JavaSDM.ensure(_TmpObject instanceof StopNode);
						stopNode = (StopNode) _TmpObject;

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}
				}
				JavaSDM.ensure(fujaba__Success);
				// iterate to-many link ownedActivityNode from activity to lastRuleNode
				fujaba__Success = false;

				fujaba__IterActivityToLastRuleNode = activity
						.getOwnedActivityNode().iterator();

				while (!(fujaba__Success)
						&& fujaba__IterActivityToLastRuleNode.hasNext()) {
					try {
						_TmpObject = fujaba__IterActivityToLastRuleNode.next();

						// ensure correct type and really bound of object lastRuleNode
						JavaSDM.ensure(_TmpObject instanceof StoryNode);
						lastRuleNode = (StoryNode) _TmpObject;

						// check negative bindings
						try {
							fujaba__Success = false;

							// iterate to-many link source from lastRuleNode to notOnFailureEdge
							fujaba__Success = false;

							fujaba__IterLastRuleNodeToNotOnFailureEdge = lastRuleNode
									.getOutgoing().iterator();

							while (!(fujaba__Success)
									&& fujaba__IterLastRuleNodeToNotOnFailureEdge
											.hasNext()) {
								try {
									notOnFailureEdge = (ActivityEdge) fujaba__IterLastRuleNodeToNotOnFailureEdge
											.next();

									// check object notOnFailureEdge is really bound
									JavaSDM.ensure(notOnFailureEdge != null);
									// attribute condition
									JavaSDM.ensure(notOnFailureEdge.getGuard() == EdgeGuard.FAILURE);

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}
							}
							JavaSDM.ensure(fujaba__Success);

							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}

						fujaba__Success = !(fujaba__Success);

						JavaSDM.ensure(fujaba__Success);

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}
				}
				JavaSDM.ensure(fujaba__Success);
				// create object lastToStop
				lastToStop = ActivitiesFactory.eINSTANCE.createActivityEdge();
				// assign attribute lastToStop
				lastToStop.setGuard(EdgeGuard.FAILURE);
				// create link
				lastToStop.setOwningActivity(activity);

				// create link
				lastToStop.setSource(lastRuleNode);

				// create link
				stopNode.getIncoming().add(lastToStop);

				fujaba__Success = true;
			} catch (JavaSDMException fujaba__InternalException) {
				fujaba__Success = false;
			}

			return;

		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void addToRessource(Activity activity, EPackage pack) {
		for (Adapter adapter : pack.eAdapters()) {
			activity.eAdapters().add(adapter);
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments)
			throws InvocationTargetException {
		switch (operationID) {
		case JANDSLPackage.TRANSFORMER___TRANSFORM__EPACKAGE_NODE:
			transform((EPackage) arguments.get(0), (Node) arguments.get(1));
			return null;
		case JANDSLPackage.TRANSFORMER___EMBED_SDM_IN_EANNOTATION__ACTIVITY_EANNOTATION:
			embedSDMInEAnnotation((Activity) arguments.get(0),
					(EAnnotation) arguments.get(1));
			return null;
		case JANDSLPackage.TRANSFORMER___CREATE_CHECK_AND_MOVE_TEMPLATE__NODE_EPACKAGE_ECLASS_EANNOTATION:
			return createCheckAndMoveTemplate((Node) arguments.get(0),
					(EPackage) arguments.get(1), (EClass) arguments.get(2),
					(EAnnotation) arguments.get(3));
		case JANDSLPackage.TRANSFORMER___ADD_RULE__EPACKAGE_ACTIVITY:
			return addRule((EPackage) arguments.get(0),
					(Activity) arguments.get(1));
		case JANDSLPackage.TRANSFORMER___ADD_ENDING__EPACKAGE_ACTIVITY:
			addEnding((EPackage) arguments.get(0), (Activity) arguments.get(1));
			return null;
		}
		return super.eInvoke(operationID, arguments);
	}

} //TransformerImpl
